#!/usr/bin/env python  
# -*- coding: utf-8 -*-  
from email.mime.text import MIMEText
from email.mime.application import MIMEApplication
from email.mime.multipart import MIMEMultipart
from smtplib import SMTP
from os.path import join as pjoin
import sys,re,urllib2,urllib,time,os,json,codecs,csv,smtplib,httplib

type = sys.getfilesystemencoding() 
reload(sys)  
sys.setdefaultencoding('utf8')


class Youtube_video:
    #开始方法
    def __init__(self):
        self.user_agent = 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'
        self.headers    = { 'User-Agent' : self.user_agent }
        current_path = os.path.abspath(__file__)
        self.father_path = os.path.abspath(os.path.dirname(current_path) + os.path.sep + ".") 
        self.channels = []
        self.channel_video = []
        self.channel_viewCount = ''

    def start(self,channel_id):
        self.channel_id = channel_id
        # UC02UVoPQeVi5yVJAMmn1Fmw,UC-MByr5LRnWmVN94Ija4XJg
        # 获取 go车志 频道数据
        self.getChannelData(channel_id)
        
        # 获取频道影片列表 items 数据
        self.getChannelItems(channel_id)

        # # 获取影片数据
        self.getVideoData()



    def getChannelData(self,channel_array):
        url             = 'https://www.googleapis.com/youtube/v3/channels'
        part            = 'snippet,contentDetails,statistics'
        key             = 'AIzaSyBS8p-aiWmI6jeCFvRZTYKgO5AkqrAXmkA'
        channel_url     = url + '?id='+ channel_array +'&part='+ part +'&key='+ key
        request         = urllib2.Request(channel_url, headers = self.headers)
        response        = urllib2.urlopen(request).read()
        channel_msg     = json.loads(response)

        self.channel_viewCount =  channel_msg['items'][0]['statistics']['subscriberCount']
        self.channels = channel_msg
        

    def getChannelItems(self,channel_array):
        url             = 'https://www.googleapis.com/youtube/v3/search'
        part            = 'snippet,id'
        key             = 'AIzaSyBS8p-aiWmI6jeCFvRZTYKgO5AkqrAXmkA'
        channel_url     = url +'?channelId='+ channel_array +'&part='+ part +'&key='+ key +'&order=date&maxResults=20'
        request         = urllib2.Request(channel_url, headers = self.headers)
        response        = urllib2.urlopen(request).read()
        channel_msg     = json.loads(response)
        
        self.channel_video = channel_msg


    def getVideoData(self):
        vide_id = ''
        for item in self.channel_video['items']:
            str_date = item['snippet']['publishedAt']
            try:  
                if(self.comparison(str_date[0:10])):
                    vide_id += item['id']['videoId'] + ','
            except:
                print u'未找到 videoId'

        
        # 获取用户数据
        url         = 'https://www.googleapis.com/youtube/v3/videos'
        video_array = vide_id
        part        = 'snippet,contentDetails,statistics'
        key         = 'AIzaSyBS8p-aiWmI6jeCFvRZTYKgO5AkqrAXmkA'
        video_url   = url + '?id='+video_array+'&part='+part+'&key='+ key
        request     = urllib2.Request(video_url, headers = self.headers)
        response    = urllib2.urlopen(request).read()
        msg         = json.loads(response)

        self.createCsv(msg)

    def comparison(self,str_date):
        st = time.strptime(str_date, '%Y-%m-%d')
        month_day = time.time()
        # 计算剩余天数
        day = int((month_day - time.mktime(st))/(60*60*24))
        judge = True if day<45 else False
       
        return judge

    def createCsv(self,items):
        on_off = True
        car_name = ''

        if (items):
            month_day = time.strftime("%m_%d")

            if(self.channel_id == 'UC-MByr5LRnWmVN94Ija4XJg'):
                car_name = 'GO車志'
            if(self.channel_id == 'UC02UVoPQeVi5yVJAMmn1Fmw'):
                car_name = '全名疯車Bar'
            if( self.channel_id == 'UCnao4xB4LPLa6oFLjbOg0oQ' and on_off ):
                car_name = '狂人日誌'
            if( self.channel_id == 'UCKEshOzNkXEAtt6h1zS0A6Q' and on_off ):
                car_name = 'Tcar試車頻道'
            if( self.channel_id == 'UCD76jE_-uOSeyC2q8jX7pVQ' and on_off ):
                car_name = 'carplay'
            if( self.channel_id == 'UCW92-RPH4xq1YqVbON4D0fg' and on_off ):
                car_name = 'ucar'

            djson = []

            for item in items['items']:
                video_item = {
                    "channel":{
                        "name":car_name,
                        "subscription": int(self.channel_viewCount),
                        "channel_id":self.channel_id
                    },
                    "commentCount": int(item['statistics']['commentCount']),
                    "viewCount": int(item['statistics']['viewCount']),
                    "publishedAt": item['snippet']['publishedAt'][0:10],
                    "likeCount": int(item['statistics']['likeCount']),
                    "dislikeCount": int(item['statistics']['dislikeCount']),
                    "title": item['snippet']['title'],
                    "video_id": item['id']
                }
                djson.append(video_item);


            # print djson
            if os.path.exists('%s/ytb_%s.json'%(self.father_path,month_day)) == False:
                with open('%s/ytb_%s.json'%(self.father_path,month_day),'wb') as f:
                    jStr = json.dumps(djson,ensure_ascii=False)
                    f.write(jStr)
            else:
                with codecs.open('%s/ytb_%s.json'%(self.father_path,month_day),'w') as f:
                    jStr = json.dumps(djson,ensure_ascii=False)
                    f.write(jStr)
        post_data(djson,car_name)

    def post_data(djson,car_name):
        print "%s数据捉取成功.进入数据上传"%car_name
        

spider = Youtube_video()

channel_id_array = [
    'UC-MByr5LRnWmVN94Ija4XJg', 
]
for i in channel_id_array:
    spider.start(i)

